﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using Legazpi.Payroll.DataAccess.Base;
using Legazpi.Payroll.DataAccess.Repositories;
using Legazpi.Payroll.DataAccess.DataObjects;
using Legazpi.Payroll.BusinessLogic.Services.Interfaces;
using Legazpi.Payroll.DataAccess.DataObjects.Enums;

namespace Legazpi.Payroll.BusinessLogic.Services
{
    public class AdjustmentService : IAdjustmentService
    {
        #region Fields

        private DbContext _dbContext;
        private IAdjustmentRepository _adjustmentRepository;
        private IDatabaseFactory _dbFactory;
        #endregion

        #region ctor

        public AdjustmentService()
            : this(new DatabaseFactory())
        {

        }

        public AdjustmentService(IDatabaseFactory dbFactory)
        {
            if (dbFactory == null) throw new ArgumentNullException("dbFactory");
            _dbFactory = dbFactory;
            DbContext = dbFactory.Get();
        }

        private DbContext DbContext
        {
            get { return _dbContext; }
            set
            {
                _dbContext = value;
                _adjustmentRepository = new AdjustmentRepository(_dbContext);
            }
        }

        #endregion

        public Adjustment GetById(int id)
        {
            return _adjustmentRepository.GetById(id);
        }

        public IEnumerable<Adjustment> GetAll()
        {
            return _adjustmentRepository.GetAll();
        }

        public IEnumerable<Adjustment> Get(int multiplier, int count)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<Adjustment> Get()
        {
            throw new NotImplementedException();
        }

        public void Add(Adjustment adjustment)
        {
            _adjustmentRepository.Add(adjustment);
        }

        public void Update(Adjustment adjustment)
        {
            _adjustmentRepository.Update(adjustment);
        }

        public void Delete(int id)
        {
            var adjustment = GetById(id);
            if (adjustment != null)
                _adjustmentRepository.Delete(adjustment);
        }

        public bool Save()
        {
            string message;
            _adjustmentRepository.Save(out message);
            return message == "Saved";
        }

        public IEnumerable<Adjustment> GetListCashAdvances()
        {
            return _adjustmentRepository
                .GetMany(x => x.AccountType == (int)AccountType.Advance);
        }
    }
}
